package cn.golxen.blog.rabbitMQ.consumer;

import cn.golxen.blog.commons.rabbitVo.AddRedisAvatarVo;
import cn.golxen.blog.commons.rabbitVo.AddRedisResumeVo;
import cn.golxen.blog.commons.rabbitVo.AddRedisSignatureVo;
import cn.golxen.blog.commons.rabbitVo.DeleteRedisByGenericKeyVo;
import cn.golxen.blog.commons.utils.RabbitMQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class AdminConsumer {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @RabbitListener(queues = "AdminQue")
    @RabbitHandler
    public void AdminHandler(RabbitMQTemplate rabbitMQTemplate){
        switch (rabbitMQTemplate.getType()){
            case "addRedisAvatar":
                addRedisAvatar((AddRedisAvatarVo)rabbitMQTemplate);
                break;
            case "addRedisResume":
                addRedisResume((AddRedisResumeVo)rabbitMQTemplate);
                break;
            case "addRedisSignature":
                addRedisSignature((AddRedisSignatureVo)rabbitMQTemplate);
                break;
            case "deleteRedisByAdmin":
                deleteRedisByAdmin((DeleteRedisByGenericKeyVo)rabbitMQTemplate);
        }
    }

    private void deleteRedisByAdmin(DeleteRedisByGenericKeyVo rabbitMQTemplate) {
        redisTemplate.delete(rabbitMQTemplate.getDelKey());
    }

    private void addRedisSignature(AddRedisSignatureVo rabbitMQTemplate) {
        redisTemplate.opsForValue().set("signature",rabbitMQTemplate.getSignature(),1, TimeUnit.DAYS);
    }

    private void addRedisResume(AddRedisResumeVo rabbitMQTemplate) {
        redisTemplate.opsForValue().set("resume",rabbitMQTemplate.getResume(),1, TimeUnit.DAYS);
    }

    private void addRedisAvatar(AddRedisAvatarVo rabbitMQTemplate) {
        redisTemplate.opsForValue().set("avatar",rabbitMQTemplate.getAvatar(),1, TimeUnit.DAYS);
    }
}
